using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System;
using System.Collections;
using System.Web.UI;
using System.IO;
using System.Configuration;
using System.Threading;
using Microsoft.SharePoint.Utilities;

namespace NikCodes.SharePoint.ClientSide
{
    public class Settings : LayoutsPageBase
    {
        protected InputFormSection JQuerySection;
        protected InputFormSection JQueryUISection;
        protected InputFormSection PrototypeSection;
        protected InputFormSection ScriptaculousSection;
        protected CheckBox JQueryCheckbox;
        protected CheckBox PrototypeCheckbox;
        protected CheckBox JQueryUICheckbox;
        protected CheckBox JQueryUIEffectsCheckbox;
        protected CheckBox JQueryUIInteractionCheckbox;
        protected CheckBox JQueryUIWidgitCheckbox;
        protected CheckBox ScriptaculousCheckbox;
        protected CheckBox ScriptaculousBuilderCheckbox;
        protected CheckBox ScriptaculousEffectsCheckbox;
        protected CheckBox ScriptaculousDragDropCheckbox;
        protected CheckBox ScriptaculousControlsCheckbox;
        protected CheckBox ScriptaculousSliderCheckbox;
        protected CheckBox ExtCheckbox;
        protected Button OkButton;

        private SPPropertyBag _webProperties;
        public SPPropertyBag WebProperties
        {
            get { return _webProperties ?? (_webProperties = Web.Properties); }
            set { _webProperties = value; }
        }


        public Settings()
        {
            Init += OnInit;
        }

        protected void OnInit(object sender, System.EventArgs e)
        {
            Load += OnLoad;
        }

        protected void OnLoad(object sender, System.EventArgs e)
        {
            if (!IsPostBack)
            {
                JQueryCheckbox.Checked = Utilities.IsSet(WebProperties, "JQuery");
                PrototypeCheckbox.Checked = Utilities.IsSet(WebProperties, "Prototype");
                JQueryUICheckbox.Checked = Utilities.IsSet(WebProperties, "JQueryUICore");
                JQueryUIEffectsCheckbox.Checked = Utilities.IsSet(WebProperties, "JQueryUIEffects");
                JQueryUIInteractionCheckbox.Checked = Utilities.IsSet(WebProperties, "JQueryUIInteraction");
                JQueryUIWidgitCheckbox.Checked = Utilities.IsSet(WebProperties, "JQueryUIWidgit");

                ScriptaculousCheckbox.Checked = Utilities.IsSet(WebProperties, "Scriptaculous");
                ScriptaculousBuilderCheckbox.Checked = Utilities.IsSet(WebProperties, "ScriptaculousBuilder");
                ScriptaculousEffectsCheckbox.Checked = Utilities.IsSet(WebProperties, "ScriptaculousEffects");
                ScriptaculousDragDropCheckbox.Checked = Utilities.IsSet(WebProperties, "ScriptaculousDragDrop");
                ScriptaculousControlsCheckbox.Checked = Utilities.IsSet(WebProperties, "ScriptaculousControls");
                ScriptaculousSliderCheckbox.Checked = Utilities.IsSet(WebProperties, "ScriptaculousSlider");

                ExtCheckbox.Checked = Utilities.IsSet(WebProperties, "Ext");
            }

            OkButton.Click += OnOkClick;

            JQueryCheckbox.AutoPostBack = JQueryUICheckbox.AutoPostBack = PrototypeCheckbox.AutoPostBack = ScriptaculousCheckbox.AutoPostBack = true;
            
            JQueryCheckbox.CheckedChanged += OnCheckChanged;
            JQueryUICheckbox.CheckedChanged += OnCheckChanged;
            PrototypeCheckbox.CheckedChanged += OnCheckChanged;
            ScriptaculousCheckbox.CheckedChanged += OnCheckChanged;

            SetCheckBoxes();
        }

        protected void OnOkClick(object sender, EventArgs e)
        {
            SaveProperty("JQuery", IsChecked(JQueryCheckbox).ToString());
            SaveProperty("Prototype", IsChecked(PrototypeCheckbox).ToString());

            SaveProperty("JQueryUICore", IsChecked(JQueryUICheckbox).ToString());
            SaveProperty("JQueryUIEffects", IsChecked(JQueryUIEffectsCheckbox).ToString());
            SaveProperty("JQueryUIInteraction", IsChecked(JQueryUIInteractionCheckbox).ToString());
            SaveProperty("JQueryUIWidgit", IsChecked(JQueryUIWidgitCheckbox).ToString());

            SaveProperty("Scriptaculous", IsChecked(ScriptaculousCheckbox).ToString());
            SaveProperty("ScriptaculousBuilder", IsChecked(ScriptaculousBuilderCheckbox).ToString());
            SaveProperty("ScriptaculousControls", IsChecked(ScriptaculousControlsCheckbox).ToString());
            SaveProperty("ScriptaculousDragDrop", IsChecked(ScriptaculousDragDropCheckbox).ToString());
            SaveProperty("ScriptaculousEffects", IsChecked(ScriptaculousEffectsCheckbox).ToString());
            SaveProperty("ScriptaculousSlider", IsChecked(ScriptaculousSliderCheckbox).ToString());

            SaveProperty("Ext", IsChecked(ExtCheckbox).ToString());

            WebProperties.Update();
            SPUtility.Redirect("settings.aspx", SPRedirectFlags.RelativeToLayoutsPage, Context);
        }

        protected void OnCheckChanged(object sender, EventArgs e)
        {
            SetCheckBoxes();
        }

        private static bool IsChecked(CheckBox checkbox)
        {
            return (checkbox.Checked && checkbox.Enabled);
        }

        private void SetCheckBoxes()
        {
            //JQuery
            JQueryUISection.Visible = JQueryUICheckbox.Enabled = JQueryCheckbox.Checked;
            JQueryUIEffectsCheckbox.Enabled = IsChecked(JQueryUICheckbox);
            JQueryUIInteractionCheckbox.Enabled = IsChecked(JQueryUICheckbox);
            JQueryUIWidgitCheckbox.Enabled = IsChecked(JQueryUICheckbox);

            //Prototype
            ScriptaculousSection.Visible = ScriptaculousCheckbox.Enabled = PrototypeCheckbox.Checked;
            ScriptaculousBuilderCheckbox.Enabled = IsChecked(ScriptaculousCheckbox);
            ScriptaculousControlsCheckbox.Enabled = IsChecked(ScriptaculousCheckbox);
            ScriptaculousDragDropCheckbox.Enabled = IsChecked(ScriptaculousCheckbox);
            ScriptaculousEffectsCheckbox.Enabled = IsChecked(ScriptaculousCheckbox);
            ScriptaculousSliderCheckbox.Enabled = IsChecked(ScriptaculousCheckbox);
        }

        private void SaveProperty(string key, string value) 
        {
            if (WebProperties.ContainsKey(Utilities.PrefixCode + key))
                WebProperties.Remove(Utilities.PrefixCode + key);

            WebProperties.Add(Utilities.PrefixCode + key, value);
        }

        protected override bool RequireSiteAdministrator
        {
            get { return true; }
        }
    }
}
